public class java5 {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String reversedStr = reverseString(str, 7, 12);
        System.out.println("反转后的字符串为: " + reversedStr);  // 输出: Hello, dlroW!
    }

    public static String reverseString(String str, int startIndex, int endIndex) {
        if (startIndex < 0 || endIndex >= str.length() || startIndex >= endIndex) {
            return "无效的索引范围";
        }

        StringBuilder sb = new StringBuilder(str);
        while (startIndex < endIndex) {
            char temp = sb.charAt(startIndex);
            sb.setCharAt(startIndex, sb.charAt(endIndex));
            sb.setCharAt(endIndex, temp);
            startIndex++;
            endIndex--;
        }

        return sb.toString();
    }
}
